package net.lab1024.sa.admin.module.business.erp.poo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.inv.dao.InventordDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SaordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordaDao;
import net.lab1024.sa.admin.module.business.erp.inv.domain.entity.InventordEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SaordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.StoordVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.StoordaVO;
import net.lab1024.sa.admin.module.business.erp.inv.domain.vo.TargetVo;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购入库单 Service
 *
 * @Author AKITA
 * @Date 2025-07-25 09:40:52
 * @Copyright AKITA
 */

@Service
public class StoordService {

    @Resource
    private StoordDao stoordDao;
    @Resource
    private StoordaDao stoordaDao;
    @Resource
    private InventordDao inventordDao;
    @Resource
    private SaordaDao saordaDao;

    /**
     * 分页查询
     */
    public PageResult<StoordVO> queryPage(StoordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<StoordVO> list = stoordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional
    public ResponseDTO<Long> add(StoordAddForm addForm) {
        StoordEntity stoordEntity = SmartBeanUtil.copy(addForm, StoordEntity.class);
        stoordDao.insert(stoordEntity);
        InventordEntity inventordEntity = new InventordEntity();
        for (StoordaEntity stoordaEntity : addForm.getLista()) {
            stoordaEntity.setId(stoordEntity.getId());
            inventordEntity.setItemId(stoordaEntity.getItemId());
            inventordEntity.setItemName(stoordaEntity.getItemName());
            inventordEntity.setBatchId(stoordaEntity.getBatchId());
            inventordEntity.setStoId(stoordaEntity.getStoId());
            inventordEntity.setStoName(stoordaEntity.getStoName());
            inventordEntity.setQty(stoordaEntity.getQty());
            inventordEntity.setSpecs(stoordaEntity.getSpecs());
            inventordEntity.setUnit(stoordaEntity.getUnit());
            inventordEntity.setQuality(stoordaEntity.getQuality());
            inventordEntity.setId(null);
            inventordEntity.setBaseId(stoordaEntity.getId());
            inventordEntity.setBaseLineNum(stoordaEntity.getLineNum());
            inventordDao.insert(inventordEntity);


            stoordaDao.insert(stoordaEntity);
        }


        /* 查询采购订单*/


        return ResponseDTO.ok(stoordEntity.getId());
    }

    /**
     * 更新
     */
    @Transactional
    public ResponseDTO<String> update(StoordUpdateForm updateForm) {

        int lineNum = 1;
        StoordEntity stoordEntity = SmartBeanUtil.copy(updateForm, StoordEntity.class);
        if (stoordDao.updateById(stoordEntity) == 0) {
            throw new RuntimeException("数据已被修改，刷新后重试！");
        }
        stoordaDao.deleteById(stoordEntity.getId());

        for (StoordaEntity stoordaEntity : updateForm.getLista()) {
            if (!stoordaEntity.getDel()) {
                stoordaEntity.setId(stoordEntity.getId());
                stoordaEntity.setLineNum(lineNum++);
                stoordaDao.insert(stoordaEntity);
            } else {
                SaordaEntity saordaEntity = saordaDao.selectByTarget(stoordaEntity.getBaseId(), stoordaEntity.getBaseLineNum());
                saordaEntity.setInvQty(saordaEntity.getInvQty().subtract(stoordaEntity.getQty()));
                saordaDao.update(saordaEntity, new LambdaQueryWrapper<SaordaEntity>().eq(SaordaEntity::getId, saordaEntity.getId()).eq(SaordaEntity::getLineNum, saordaEntity.getLineNum()));
                inventordDao.delete(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getBatchId, stoordaEntity.getBatchId()));
            }
        }
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        stoordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long id) {
        List<StoordaEntity> stoordaEntities = stoordaDao.selectList(new LambdaQueryWrapper<StoordaEntity>().eq(StoordaEntity::getId, id));
        for (StoordaEntity stoordaEntity : stoordaEntities) {
            InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getBatchId, stoordaEntity.getBatchId()));

            if (inventordEntity.getQty().compareTo(stoordaEntity.getQty()) == 0) {
                inventordDao.deleteById(inventordEntity.getId());
            } else {
                return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "物料已经被消耗，禁止删除！");
            }
        }
        stoordDao.deleteById(id);
        stoordaDao.deleteById(id);
        return ResponseDTO.ok();
    }


    public ResponseDTO<StoordVO> info(Long id) {

        StoordEntity stoordEntity = stoordDao.selectById(id);
        StoordVO copy = SmartBeanUtil.copy(stoordEntity, StoordVO.class);
        List<StoordaEntity> stoordaEntities = stoordaDao.selectList(new LambdaQueryWrapper<StoordaEntity>().eq(StoordaEntity::getId, id));
        List<StoordaVO> stoordaVOS = SmartBeanUtil.copyList(stoordaEntities, StoordaVO.class);
        ArrayList<TargetVo> targetVob = new ArrayList<>();

        for (StoordaEntity item : stoordaEntities) {
            if (item.getBaseId() != null) {
                TargetVo targetVo = new TargetVo();
                targetVo.setId(item.getBaseId());
                targetVo.setLineNum(item.getBaseLineNum());
                targetVo.setItemId(item.getItemId());
                targetVo.setItemName(item.getItemName());
                targetVo.setQty(item.getQty());
                targetVob.add(targetVo);
            }
        }

//        List<SubordaEntity> subordaEntities = subordaDao.selectList(new LambdaQueryWrapper<SubordaEntity>().eq(SubordaEntity::getBaseId, id));


//
//
        ArrayList<TargetVo> targetVoc = new ArrayList<>();
//        for (SubordaEntity item : subordaEntities) {
//            TargetVo targetVo = new TargetVo();
//            targetVo.setId(item.getId());
//            targetVo.setLineNum(item.getLineNum());
//            targetVo.setItemId(item.getItemId());
//            targetVo.setItemName(item.getItemName());
//            targetVo.setQty(item.getQty());
//            targetVoc.add(targetVo);
//        }

        copy.setLista(stoordaVOS);
        copy.setListb(targetVob);
        copy.setListc(targetVoc);

        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<StoordVO> infoUnexecute(Long id) {
        StoordEntity stoordEntity = stoordDao.selectById(id);
        StoordVO copy = SmartBeanUtil.copy(stoordEntity, StoordVO.class);
        List<StoordaVO> puordaVOList = stoordaDao.infoUnexecute(id);
        copy.setLista(puordaVOList);
        return ResponseDTO.ok(copy);
    }
}
